package com.isoft.hospital.service.Impl;

import com.isoft.hospital.common.ResponseData;
import com.isoft.hospital.dao.DepartmentDao;
import com.isoft.hospital.entity.Department;
import com.isoft.hospital.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentDao dao;

    @Override
    public ResponseData selectAll() {
        List<Department> list = dao.selectAll();
        ResponseData responseData = new ResponseData();
        if (list != null) {
            responseData.setCode(0);
            responseData.setMsg("ok");
            responseData.setData(list);
        } else {
            responseData.setCode(1);
            responseData.setMsg("no");
        }
        return responseData;
    }

    @Override
    public ResponseData insert(Department department) {
        int temp = dao.insert(department);
        ResponseData responseData = new ResponseData();
        if (temp > 0) {
            responseData.setCode(0);
            responseData.setMsg("ok");
        } else {
            responseData.setCode(1);
            responseData.setMsg("no");
        }
        return responseData;
    }

    @Override
    public ResponseData delete(int d_id) {
        int temp = dao.delete(d_id);
        ResponseData responseData = new ResponseData();
        if (temp > 0) {
            responseData.setCode(0);
            responseData.setMsg("ok");
        } else {
            responseData.setCode(1);
            responseData.setMsg("no");
        }
        return responseData;
    }

    @Override
    public ResponseData update(Department department) {
        int temp = dao.update(department);
        ResponseData responseData = new ResponseData();
        if (temp > 0) {
            responseData.setCode(0);
            responseData.setMsg("ok");
        } else {
            responseData.setCode(1);
            responseData.setMsg("no");
        }
        return responseData;
    }

    @Override
    public ResponseData findById(int d_id) {
        Department department=dao.findById(d_id);

        ResponseData responseData = new ResponseData();
        if (department!=null) {
            responseData.setCode(0);
            List<Department> departments=new ArrayList<>();
            departments.add(department);
            responseData.setMsg("ok");
            responseData.setData(departments);
        } else {
            responseData.setCode(1);
            responseData.setMsg("no");
        }
        return responseData;

    }
}

